package com.xujieyang.study.thrift;

import com.xujieyang.study.thrift.HelloWorldService.AsyncClient.sayHello1_call;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.*;

import java.io.IOException;

public class Client {
	
	public static void main(String[] args) {
		clientSimple();
	}
	
	public static void clientSimple() {
		TTransport transport = null;
		try {
			transport = new TSocket("localhost", 6000);
			// 协议要和服务端一致
			TProtocol protocol = new TBinaryProtocol(transport);
			HelloWorldService.Client client = new HelloWorldService.Client(protocol);
//			HelloWorldService.AsyncClient asyncClient = new HelloWorldService.AsyncClient(protocol);
			transport.open();
			String str = client.sayHello1("11111");
			System.out.println("Thrify client result =: " + str);
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		} finally {
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
	}
	
	public static void clientNonblocking() {
		TNonblockingTransport transport = null;
		try {
			TAsyncClientManager clientManager = new TAsyncClientManager();
			transport = new TNonblockingSocket("localhost", 60000);
			TProtocolFactory protocol = new TCompactProtocol.Factory();
			// 协议要和服务端一致
			HelloWorldService.AsyncClient asyncClient = new HelloWorldService.AsyncClient(protocol, clientManager, transport);
//			transport.open();
			asyncClient.sayHello1("111", new AsyncMethodCallback<sayHello1_call>() {

				@Override
				public void onComplete(sayHello1_call response) {
					try {
						System.out.println(response.getResult());
					} catch (TException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onError(Exception exception) {
					
				}
			});
		} catch (TTransportException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
	}
	
	public static void main_syn() {  
        TTransport transport = new TFramedTransport(new TSocket("localhost", 6000));  
        TProtocol protocol = new TCompactProtocol(transport);  
        HelloWorldService.Client client = new HelloWorldService.Client(protocol);
        try {  
            transport.open();  
            System.out.println(client.sayHello1("11111"));  
        } catch (TApplicationException e) {  
            System.out.println(e.getMessage() + " " + e.getType());  
        } catch (TTransportException e) {  
            e.printStackTrace();  
        } catch (TException e) {  
            e.printStackTrace();  
        }  
        transport.close();  
    }  
	
	public static void main_asy() { 
		TNonblockingTransport transport = null;
        try {  
            TAsyncClientManager clientManager = new TAsyncClientManager();  
            transport = new TNonblockingSocket("localhost", 6000);  
            TProtocolFactory protocol = new TCompactProtocol.Factory();  
            HelloWorldService.AsyncClient asyncClient = new HelloWorldService.AsyncClient(protocol, clientManager, transport);  
            System.out.println("Client calls .....");  
            asyncClient.sayHello1("11111", new MyCallback());
//            while (true) {
//            	Thread.sleep(1);  
//            } 
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (TException e) {
			e.printStackTrace();
		} 
//        catch (InterruptedException e) {
//			e.printStackTrace();
//		}  
//        finally {
//			transport.close();
//		}
    }  
	
	static class MyCallback implements AsyncMethodCallback<sayHello1_call> {

		@Override
		public void onComplete(sayHello1_call response) {
			System.out.println("onComplete");
			try {
				System.out.println(response.getResult().toString());
			} catch (TException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onError(Exception exception) {
			System.out.println("onError");
		}
		
	}
	
	
}
